package com.lyloou.practicerabbitmq.controller;

import cn.hutool.json.JSONUtil;
import com.lyloou.practicerabbitmq.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author lilou
 * @since 2021/2/18
 */
@Controller
public class OrderController {
    @Autowired
    OrderService orderService;

    @RequestMapping("/order")
    public String order(Model model) {
        try {
            String orderId = orderService.order();
            model.addAttribute("orderId", orderId);
            return "order";
        } catch (Exception e) {
            return "failorder";
        }
    }

    @RequestMapping("/failorder/{orderId}")
    public String failOrder(@PathVariable String orderId, Model model) {
        model.addAttribute("orderId", orderId);
        return "fail";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list() {
        return JSONUtil.toJsonPrettyStr(orderService.listOrder());
    }
}
